《剑指offer》-- 树的子结构、二叉树的镜像、二叉树的深度、平衡二叉树
生活随笔
收集整理的這篇文章主要介紹了
《剑指offer》-- 树的子结构、二叉树的镜像、二叉树的深度、平衡二叉树
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、 樹的子結構:
1、題目:
輸入兩棵二叉樹A,B,判斷B是不是A的子結構。(ps:我們約定空樹不是任意一個樹的子結構。
2、解題思路:
這個題比較簡單,利用遞歸的方式就可以判斷B是不是A樹的子結構。
3、實現代碼:
public boolean HasSubtree(TreeNode root1,TreeNode root2) {//當tree1和tree2都不為空的時候,才進行比較。否則直接返回falseif(root1 ==null || root2 == null){return false;}以這個根節點為、根節點左子樹、右子樹 為起點判斷是否包含Tree2return (isSubtree(root1,root2) || HasSubtree(root1.left,root2) || HasSubtree(root1.right, root2));}private boolean isSubtree(TreeNode root1, TreeNode root2) {//以下兩個if的順序不能調換//如果tree2已經遍歷完了,表示都可以對應上,返回trueif(root2==null)return true;//如果tree2已經遍歷完,tree1還沒遍歷完,返回falseif(root1==null)return false;//如果根節點對應的上,那么就分別去子節點里面匹配if(root1.val ==root2.val){return (isSubtree(root1.left,root2.left) && isSubtree(root1.right, root2.right));}else{return false;}}?
?
二、二叉樹的鏡像:
1、題目:
操作給定的二叉樹,將其變換為源二叉樹的鏡像。
2、輸入描述:
二叉樹的鏡像定義:源二叉樹 8/ \6 10/ \ / \5 7 9 11鏡像二叉樹8/ \10 6/ \ / \11 9 7 53、解決思路:
采用遞歸的方式,遞歸交換每一個父節點的兩個子節點的位置。
4、實現代碼:
/** public class TreeNode {int val = 0;TreeNode left = null;TreeNode right = null;public TreeNode(int val) {this.val = val;} } */ public class Solution {public void Mirror(TreeNode root) {if(root==null)return ;if(root.left==null && root.right==null)return;TreeNode temp=root.left;root.left=root.right;root.right=temp;Mirror(root.left);Mirror(root.right);} }?
?
三、二叉樹的深度:
1、題目:
輸入一棵二叉樹,求該樹的深度。從根結點到葉結點依次經過的結點(含根、葉結點)形成樹的一條路徑,最長路徑的長度為樹的深度。
2、解決思路:
遞歸方式和層序遍歷方式都可以解決。
3、代碼實現:
public class Test3 {//第二種:非遞歸方式:層序遍歷:public int TreeDepth(TreeNode root) {if(root == null){return 0;}Queue<TreeNode> queue = new LinkedList<TreeNode>();queue.add(root);//depth代表當前節點所在的層數,count代表已經遍歷的節點數,nextCount代表下一層的節點數//當count==nextCount的時候,代表本次節點已經遍歷完畢int depth=0,count=0,nextCount=1;while(queue.size()!=0){count++;TreeNode top = queue.poll();if(top.left!=null){queue.add(top.left);}if(top.right!=null){queue.add(top.right);}if(count==nextCount){depth++;count=0;nextCount=queue.size();}}return depth;}//第一種:遞歸方式public int TreeDepth1(TreeNode root) {if(root == null){return 0;}int left= TreeDepth1(root.left);int right =TreeDepth1(root.right);return Math.max(left, right)+1;} }class TreeNode {int val = 0;TreeNode left = null;TreeNode right = null;public TreeNode(int val) {this.val = val;} }?
?
四、平衡二叉樹:
1、題目:
輸入一棵二叉樹,判斷該二叉樹是否是平衡二叉樹。
2、解題思路:
遞歸方式或者后序遍歷的方式都可以解決。
3、代碼實現:
public class Test5 {private boolean isBalanced = true;//第三種:后續遍歷時,遍歷到一個節點,其左右子樹已經遍歷 依次自底向上判斷,每個節點只需要遍歷一次public boolean IsBalanced_Solution2(TreeNode root) {getDepth2(root);return isBalanced;}public int getDepth2(TreeNode root){if(root == null){return 0;}int left=getDepth2(root.left);int right=getDepth2(root.right);if(Math.abs(left-right)>1){isBalanced=false;}return Math.max(left, right)+1;}//第二種方法:三種中最好的方式//如果改為從下往上遍歷,如果子樹是平衡二叉樹,則返回子樹的高度;如果發現子樹不是平衡二叉樹,//則直接停止遍歷,這樣至多只對每個結點訪問一次。public boolean IsBalanced_Solution1(TreeNode root) {return getDepth(root) !=-1;}public int getDepth(TreeNode root){if(root == null) return 0;int left=getDepth(root.left);if(left == -1 ) return -1;int right=getDepth(root.right);if(right == -1) return -1;return Math.abs(left-right)>1?-1:1+Math.max(left, right);}//第一種方法:遞歸方式//這種做法有很明顯的問題,在判斷上層結點的時候,會多次重復遍歷下層結點,增加了不必要的開銷。public boolean IsBalanced_Solution(TreeNode root) {if(root == null){return true;}return Math.abs(maxDepth(root.left)-maxDepth(root.right))<=1&& IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);}private int maxDepth(TreeNode root){if(root == null){return 0;}return 1+Math.max(maxDepth(root.left), maxDepth(root.right));} }class TreeNode {int val = 0;TreeNode left = null;TreeNode right = null;public TreeNode(int val) {this.val = val;} }?
總結
以上是生活随笔為你收集整理的《剑指offer》-- 树的子结构、二叉树的镜像、二叉树的深度、平衡二叉树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《剑指offer》-- 链表中倒数第k个
- 下一篇: 《剑指offer》-- 调整数组顺序使奇