您好,登錄后才能下訂單哦!
小編給大家分享一下Java算法中二叉樹的練習(xí)題有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
解法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { int i = 0; int res = 0; public int kthSmallest(TreeNode root, int k) { method(root,k); return res; } public void method(TreeNode root, int k){ if(root==null) return ; method(root.left,k); i++; if(i==k){ res = root.val; return ; } method(root.right,k); } }
解法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { int sum = 0; public TreeNode convertBST(TreeNode root) { method(root); return root; } public void method(TreeNode root) { if(root==null){ return; } method(root.right); sum+=root.val; root.val = sum; method(root.left); } }
解法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isValidBST(TreeNode root) { return method(root,null,null); } public boolean method(TreeNode root,TreeNode min,TreeNode max){ if(root==null) return true; if(min!=null&&root.val<=min.val) return false; if(max!=null&&root.val>=max.val) return false; return method(root.left,min,root)&&method(root.right,root,max); } }
解法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode searchBST(TreeNode root, int val) { if(root==null) return null; if(root.val==val) return root; if(root.val>=val){ return searchBST(root.left,val); } if(root.val<val){ return searchBST(root.right,val); } return null; } }
解法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode insertIntoBST(TreeNode root, int val) { return method(root,val); } public TreeNode method(TreeNode root, int val){ if(root==null) return new TreeNode(val); if (root.val < val) root.right = insertIntoBST(root.right, val); if (root.val > val) root.left = insertIntoBST(root.left, val); return root; } }
算法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode deleteNode(TreeNode root, int key) { if (root == null) return null; if (root.val == key){ if (root.left == null) return root.right; if (root.right == null) return root.left; TreeNode minNode = getMin(root.right); root.right = deleteNode(root.right, minNode.val); minNode.left = root.left; minNode.right = root.right; root = minNode; }else if(root.val>key){ root.left = deleteNode(root.left,key); }else{ root.right = deleteNode(root.right,key); } return root; } TreeNode getMin(TreeNode node) { while (node.left != null) node = node.left; return node; } }
以上是“Java算法中二叉樹的練習(xí)題有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。