leetcode 1: balanced binary tree

来源:互联网 发布:网络主播工资待遇 编辑:程序博客网 时间:2024/06/05 05:26

Balanced Binary TreeOct 9 '12

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 everynode never differ by more than 1.

思路: 注意, 本题的balanced tree 不是通用定义的, 最短subtree 和 最长的subtree是有可能 高度相差2的, 只要他们不共享一个root, 按本题的定义,也是balanced.

public class Solution {    public boolean isBalanced(TreeNode root) {        return rec(root)!=-1;    }        private int rec(TreeNode root) {        if(root==null) return 0;        int l = rec(root.left);        int r = rec(root.right);        if(l==-1 || r==-1 || Math.abs(l-r) > 1) return -1;        return Math.max(l, r)+1;    }}


/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ #include <algorithm> class Solution {public:    int height( TreeNode *root) {        if(root==NULL) return 0;                return 1+ max( height(root->left), height(root->right) );            }    bool isBalanced(TreeNode *root) {        if(root == NULL) return 1;                if( isBalanced(root->left) && isBalanced(root->right) && abs( height(root->left) - height(root->right) ) <=1) {            return 1;        } else {            return 0;        }      }  };


public class Solution {    public boolean isBalanced(TreeNode root) {        // Start typing your Java solution below        // DO NOT write main() function        //input check        if(root==null) return true;                return balRec(root)!=-1;    }        private int balRec(TreeNode root) {        if(root==null) return 0;                int l = balRec(root.left);        int r = balRec(root.right);                if(l==-1 || r==-1) return -1;                if( Math.abs(l-r)>1) {            return -1;        } else {            return Math.max(l, r) +1;        }       }   }

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public boolean isBalanced(TreeNode root) {        // Start typing your Java solution below        // DO NOT write main() function        //input check        if(root==null) return true;                return balRec(root)!=-1;    }        private int balRec(TreeNode root) {        if(root==null) return 0;                int l = balRec(root.left);        int r = balRec(root.right);                if(l==-1 || r==-1) return -1;                if( Math.abs(l-r)>1) {            return -1;        } else {            return Math.max(l, r) +1;        }       }   }

interesting error. actually , the shortest path and longest path of balanced binary tree can be different at most 2.

/** * 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) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        if(root==nullptr) return true;                queue<TreeNode*> que;        que.push(root);        int q1=1, q2=0;        int min_level =-1;        int level = 0;        while(!que.empty()) {           TreeNode* n = que.front();           que.pop();           --q1;                      if(min_level==-1 && (n->left==nullptr || n->right==nullptr) ){               min_level = level;           }                       if(min_level!=-1 && min_level<level-1) {               return false;           }                      if(n->left!=nullptr) {               que.push(n->left);               ++q2;           }                      if(n->right!=nullptr) {               que.push(n->right);               ++q2;           }                      if(q1==0) {               swap(q1,q2);               ++level;           }        }        return true;    }};


原创粉丝点击