Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node
递归思路:
(1)若一棵树只有一个节点,则其深度为1;
(2)若一棵树只有左子树而没有右子树,则树的深度应该是其左子树的深度加一,相反,则树的深度是其右子树加一;
(3)若一棵树既存在左子树又存在右子树,则树的深度应该是其左、右子树中较大值加一;
写法1:
int maxDepth(Node* root) {
if (root == NULL)
return 0;
int nLeft = maxDepth(root->left);
int nRight = maxDepth(root->right);
return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);
}
写法2:
深度优先搜索:
int treeDepth(TreeNode *root)
{
return root == NULL ? 0 : max(tree(maxDepth(root -> left), treeDepth(root -> right)) + 1;
}
扩展一:
- Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.
https://leetcode.com/problems/trim-a-binary-search-tree/description/
public TreeNode trimBST(TreeNode root, int L, int R) {
if(root == null) return null;
if(root.val >= L && root.val <= R) {
root.left = trimBST(root.left, L, R);
root.right = trimBST(root.right, L, R);
return root;
}
if(root.val < L) return trimBST(root.right, L, R);
if(root.val > R) return trimBST(root.left, L, R);
}
解题思路:
如果当前 root 正好在范围之内,那么把问题递归到它的左结点和右结点。
如果当前 root 不在范围内,比 L 小,那么 它和它的左子树 可以被抛弃了。
如果当前 root 不在范围内,比 R 大,那么 它和它的右子树 可以被抛弃了。
方法2:
广度优先搜索
int treeDepth(TreeNode* root) {
if(root == NULL)
return 0;
queue<TreeNode*> q;
int ret = 0;
q.push(root);
while(!q.empty())
{
++ret;
for(int i = 0, n = q.size(); i < n; i++)
{
TreeNode *current = q.front();
q.pop();
if(current->right != NULL)
q.push(current->right);
if(current->left != NULL)
q.push(current->left);
}
}
return ret;
}
《剑指office》:二叉树的深度
判断一棵树是否是平衡二叉树(左右子树深度相差不大于1):
bool isBalanceTree(Node* root)
{
if (root == NULL)
return true;
int nLeft = treeDepth(root->left);
int nRight = treeDepth(root->right);
int d = nLeft - nRight;
if (d > 1 || d < -1)
return false;
return isBalanceTree(root->right) && isBalanceTree(root->left);
}
效率不高:
先判断根节点是不是平衡的,再判断root->right、root->left 是不是平衡的,treeDepth存在重复遍历4、5、7结点问题
bool isBalance(Node *current, int &d)
{
if (current == NULL)
{
d = 0;
return true;
}
int l, r;
//后序遍历,并记录子树的深度
if (isBalance(current->left, l) && isBalance(current->right, r))
{
int d1 = l - r;
if (d1 <= 1 && d1 >= -1)
{
d = 1 + (l > r ? l : r);
return true;
}
}
}
bool isBalance(Node* root)
{
int d = 0;
return isBalance(root, d);
}
完整版:
#include<iostream>
#include<queue>
using namespace std;
struct Node {
int val;
Node *left;
Node *right;
Node(int v = 0, Node *l = NULL, Node *r = NULL)
:val(v), left(l), right(r) {}
};
struct Tree {
Tree(int a[], int len);
Node *root;
void createTree(Node *current, int val);
void createTree(int val);
};
void Tree::createTree(int val)
{
if (root != NULL)
createTree(root, val);
}
void Tree::createTree(Node *current, int val)
{
if (val < current->val)
{
if (current->left == NULL)
current->left = new Node(val);
else
createTree(current->left,val);
}
else if (val > current->val)
{
if (current->right == NULL)
current->right = new Node(val);
else
createTree(current->right, val);
}
}
Tree::Tree(int a[], int len)
{
root = new Node(a[0]);
for (int i = 1; i < len; i++)
{
createTree(a[i]);
}
}
int maxDepth(Node* root) {
if (root == NULL)
return 0;
int nLeft = maxDepth(root->left);
int nRight = maxDepth(root->right);
return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);
}
int main()
{
int i = 1024;
int a[9] = { 5, 3, 2, 4, 1, 6, 8, 7, 9 };
Tree t(a, 9);
cout << maxDepth(t.root) << endl;
return 0;
}