本篇文章主要对树的常见的算法做一个总结,如果知道思路,写起来代码是非常简单的,不了解的话可能代码会又臭又长,标题中的4类问题在本文中均由DFS实现。
1、求树的最小深度
给出一道leetcode题目
111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int min_;
void dfs(TreeNode* p,int depth)
{
if (p->left!=NULL ) //左子非空,带入左子且长度+1
dfs(p->left, depth + 1);
if(p->right!=NULL) //右子同理
dfs(p->right,depth+1);
if (p->left == NULL && p->right==NULL)//注意这里的判断很重要,不能递归到p==NULL,这样的话是无法判断有没有到达根节点的,可能是某个节点的没有某个子节点也可能会这样
//此时访问到末端节点,若得出的长度比当前min小,赋给min
min_ = min(min_,depth);
}
int minDepth(TreeNode* root) {
min_=INT_MAX;
if(root == NULL)return 0;
dfs(root,1);
return min_;
}
};
还有另一种看起来更简便的的写法
int minDepth(TreeNode root) {
if(root==null) return 0; //1
else if(root.left!=null && root.right !=null) return 1 + Math.min(minDepth(root.left), minDepth(root.right)); //2
else return 1 + minDepth(root.left) + minDepth(root.right); //3
//最后一行中minDepth(root.left)和minDepth(root.right)至少一个为0
}
2、平衡树的判定
同样给出leetcode题目
110. Balanced Binary Tree
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.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int dfs(TreeNode* root)
{
if(root==NULL)return 0;
int leftHeight = dfs(root->left);
if(leftHeight==-1)return -1;
int rightHeight = dfs(root->right);
if(rightHeight==-1)return -1;
if(abs(leftHeight-rightHeight)>1)return -1;
return max (leftHeight, rightHeight) + 1;
}
bool isBalanced(TreeNode* root) {
if(dfs(root)!=-1)
return true;
else return false;
}
};
3、两颗树相同的判定
100Same Tree
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
需要注意的一点是除了判断指针指向的val是否相同外,还有判断两个指针本身是否相同。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool dfs(TreeNode* p, TreeNode* q){
if(p==NULL||q==NULL)return p==q;
return (p->val==q->val&&dfs(p->left,q->left)&&dfs(p->right,q->right));
}
bool isSameTree(TreeNode* p, TreeNode* q) {
return dfs(p,q);
}
};
4、判断树自身是否对称
101Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
利用3中的判定两树相同的算法即可,稍微做一点改变。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool dfs(TreeNode* p, TreeNode* q){
if(p==NULL||q==NULL)return p==q;
return (p->val==q->val&&dfs(p->left,q->right)&&dfs(p->right,q->left));
}
bool isSymmetric(TreeNode* root) {
if(root==NULL)return true;
return dfs(root->left,root->right);
}
};