【Leetcode】Balanced Binary Tree

来源:互联网 发布:什么牌子棒球帽知乎 编辑:程序博客网 时间:2024/06/06 18:47

题目:

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

解题思路1(下面给出两种代码实现,代码1和代码2):采用递归的方法,对每一层的节点进行遍历,从叶子节点开始回溯,并从下往上判断每一层的各个是否满足平衡树的条件,并得到以该节点为根节点的左右子树的深度,进而得到该节点的深度以便于上层的判决。

代码1:

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    bool isBalanced(TreeNode *root) {        if(!root)return true;        int depth;        return CheckBal(root,&depth);    }private:    bool CheckBal(TreeNode *root, int *depth){        if(root==nullptr){            *depth=0;            return true;        }        int depth_left=0,depth_right=0;        if(!CheckBal(root->left,&depth_left)||!CheckBal(root->right,&depth_right)||abs(depth_left-depth_right)>1){            return false;        }        *depth=max(depth_left,depth_right)+1;        return true;    }};


代码2:这段代码是我一开始写的代码,看起来要长不少。因为代码中有一个问题——将左右子树作为两个参数传入函数中,导致函数在运行时需要对左右两颗子树分别作处理,其实可以归并为同一段代码,只需要将根节点作为参数放入函数然后分别对左右子树递归处理即可。(如同代码1所示)

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    bool isBalanced(TreeNode *root) {        if(!root)return true;        int root_left=0,root_right=0;        return CheckBal(root->left,root->right,&root_left,&root_right);    }private:    bool CheckBal(TreeNode *Left,TreeNode *Right, int *depth_l,int *depth_r){        if(Left==nullptr&&Right==nullptr){            return true;        }        int depth_l_l=0,depth_l_r=0,depth_r_l=0,depth_r_r=0;        if(Left){            (*depth_l)++;            if(CheckBal(Left->left,Left->right,&depth_l_l,&depth_l_r)){                (*depth_l)+=max(depth_l_l,depth_l_r);            }else{                return false;            }        }        if(Right){            (*depth_r)++;            if(CheckBal(Right->left,Right->right,&depth_r_l,&depth_r_r)){                (*depth_r)+=max(depth_r_l,depth_r_r);            }else{                return false;            }        }        if(((*depth_l)+1<*depth_r)||((*depth_r)+1<*depth_l)){            return false;        }else{            return true;        }    }};



解题思路2:由于是深度是非负的,因此可以通过-1来表示不平衡。从而避免了返回值不一致的问题(如第一种思路,为了让返回值为bool型并记录深度,不得不采取了传引用的函数调用方式)。


代码:

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    bool isBalanced(TreeNode *root) {        return TreeHeight(root)!=-1;    }private:    int TreeHeight(TreeNode *root){        if(!root)return 0;                int H_left=TreeHeight(root->left);        int H_right=TreeHeight(root->right);                if((H_left==-1)||(H_right==-1)||(abs(H_left-H_right)>1)){            return -1;        }                return max(H_left,H_right)+1;    }};


0 0
原创粉丝点击