39 平衡二叉树(深度+非递归/递归+后序)

题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。

import java.util.*;
public class Solution {
    public int _Depth_Solution(TreeNode root) {
        if(root == null)
            return 0;
        int left = _Depth_Solution(root.left) + 1;
        int right = _Depth_Solution(root.right) + 1;
        return (left > right) ? left : right;
    }
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root == null)
            return true;
        Stack<TreeNode> stack = new Stack();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode cur = stack.pop();
            int l = _Depth_Solution(cur.left);
            int r = _Depth_Solution(cur.right);        
            if(Math.abs(l-r) > 1)
                return false;
            if(cur.right != null)
                stack.push(cur.right);
            if(cur.left != null)
                stack.push(cur.left);            

        }
        return true;
    }
}

上面的这种做法很渣。

需要重复遍历节点多次。

递归的写法:

import java.util.*;
public class Solution {
    public int _Depth_Solution(TreeNode root) {
        if(root == null)
            return 0;
        int left = _Depth_Solution(root.left) + 1;
        int right = _Depth_Solution(root.right) + 1;
        return (left > right) ? left : right;
    }
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root == null)
            return true;
        int l = _Depth_Solution(root.left);
        int r = _Depth_Solution(root.right);        
        if(Math.abs(l-r) > 1)
            return false;        
        return IsBalanced_Solution(root.left)&& IsBalanced_Solution(root.right);
    }
}

核心:判断当前节点的左右孩子的深度差是否大于1,若是,则不是平衡树!
然后,根据这一原则遍历所有节点即可!

使用后续遍历的方式,先遍历两子节点,再判断当前节点。在遍历每个节点的时候记录当前节点的深度,就可以一边遍历,一边记录它的深度

import java.util.*;
public class Solution {    
     public boolean _IsBalanced_Solution(TreeNode root,int[] depth) {
         if(root == null){
             depth[0] = 0;
             return true;
         }
         int[] left = new int[1],right = new int[1];
         if(_IsBalanced_Solution(root.left,left) && _IsBalanced_Solution(root.right,right)){
             if(Math.abs(left[0] - right[0]) <= 1){
                 depth[0] = ((left[0] > right[0]) ? left[0] : right[0]) + 1;
                 return true;
             }
         }
         return false;
     }
    public boolean IsBalanced_Solution(TreeNode root) {
        int[] depth = new int[1];
        return _IsBalanced_Solution(root,depth); 
    }
}

写成这样更容易理解:

  private boolean _IsBalanced_Solution(TreeNode root, int[] depth) {
        if(root == null){
            depth[0] = 0;
            return true;
        }
        int[] leftDepth = new int[1],rightDepth = new int[1];
        boolean leftB = _IsBalanced_Solution(root.left,leftDepth);
        boolean rightB = _IsBalanced_Solution(root.right,rightDepth);
        if(leftB && rightB){
            depth[0] = ((leftDepth[0] > rightDepth[0]) ? 
                                               leftDepth[0] : rightDepth[0]);
            return  true;
        }
        return false;
    }

参考:
https://blog.csdn.net/abc7845129630/article/details/52744985
https://blog.csdn.net/Zheng548/article/details/65935030
注意:传递的必须是数组,引用传递。如果使用的是int类型的话,只是值传递,由于返回类型是boolean,所以不能把当前深度传递回来!

    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/xuchonghao/article/details/80351720
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞