深度和高度的区别:
- 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
- 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始)
LeetCode104二叉树的最大深度
思路:后序遍历,将左右节点得到的深度返回给父节点。
关键点:别忘了对父节点加1.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
// 后序遍历
if(root == null){
return 0;
}
int left = maxDepth(root.left); // 得到左子节点的深度
int right = maxDepth(root.right); // 得到右子节点的深度
int max = Math.max(left,right)+1; // 要加1,返回的是节点数。不是边的数量,20这个节点,15和7返回的都是1。
return max;
}
}
发现一个问题:在面试的时候手写代码的话,前面的这些数据结构有可能都是要自己去写的。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
int ans = getDeep(root);
return ans;
}
int getDeep(TreeNode* node){
if(node == NULL){
return 0;
}
int left = getDeep(node->left) + 1;
int right = getDeep(node->right) + 1;
int res = max(left, right);
return res;
}
};
LeetCode559:N叉数的最大深度
思路:和二叉树的差不多。就是不用后续遍历了。递归求每一个root.children的深度。
不要被给的root的样例误导了。给的是数组,不是说要对数组进行处理,是按照数组的顺序 一步步深层数。所以处理的时候还是对数的结构进行处理。
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public int maxDepth(Node root) {
// 不要被样例给的输入root误导了。他只是又来展示树,并不是说,要对数组进行处理。
// 最后还是对数的结构进行处理。root.children
if(root == null){
return 0;
}
int depth = 0;
if(root.children != null){
for(Node child : root.children){
depth = Math.max(depth, maxDepth(child));
}
}
return depth+1;
}
}
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children; // 这里是一个数组,因此要按照数组的方式处理。
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
int maxDepth(Node* root) {
queue<Node*> que;
if(root == NULL) return 0;
int ans = 0;
que.push(root);
while(!que.empty()){
int size = que.size();
for(int i=0;i<size;i++){
Node* node = que.front();
que.pop();
// 这里的代码蛮重要的
for(int j=0;j<node->children.size();j++){
if(node->children[j]) que.push(node->children[j]);
}
}
ans++;
}
return ans;
}
};
LeetCode111:二叉树的最小深度
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
注意,要分三种情况讨论。
因为,如果不讨论的话,会将左子树为空直接设置为0,并返回。这样的话如果是全部都只有右节点的话。最后的最小深度也只是1。
思路:三种情况:左子树为空、右子树为空,左右子树都不为空。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
// 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
if(root ==null){
return 0;
}
// 与最大深度的不同
// 在左子节点或右子节点为空时,要另外处理,不是直接令其为0。
// 这样的话会直接用0加上1.
// 实际上是在左子树为空的时候要去看右子树的最小深度
// 所以要分三种情况;左子树为空、右子树为空、左右子树都不为空
int left = minDepth(root.left);
int right = minDepth(root.right);
if(left==0){ // 左子树为空,返回右子树最小深度+1(+1为加上父节点直接返回)
return right+1;
}
else if(right==0){
return left+1;
}
int min = Math.min(left, right) + 1;
return min;
}
}
看Java代码部分的解释:
三种情况:左子树为空、右子树为空,左右子树都不为空。
class Solution {
public:
int minDepth(TreeNode* root) {
int min = getMinDepth(root);
return min;
}
int getMinDepth(TreeNode* node){
if(node == NULL){
return 0;
}
int left = getMinDepth(node->left);
int right = getMinDepth(node->right);
//
if(node->left == NULL && node->right != NULL){
return right + 1;
}
if(node->left != NULL && node->right == NULL){
return left + 1;
}
int minDepth = min(left, right) + 1;
return minDepth;
}
};
LeetCode222:完全二叉树的节点个数
解法一:这个是根据求深度的方法改变的。
思路:修改求深度的时候左右的最大深度+1–>每次都计数一个。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int countNodes(TreeNode root) {
// 求树深的题目改变而来。
if(root == null){
return 0;
}
int left = countNodes(root.left);
int right = countNodes(root.right);
int number = left + right + 1; // 改变这个,实现不同的功能。
return number;
}
}
解法二:基于层序遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int countNodes(TreeNode root) {
// 迭代法,利用层序遍历
if(root == null) return 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int ans =0;
while(!queue.isEmpty()){
int size = queue.size();
for(int i=0;i<size;i++){
TreeNode temp = queue.poll();
ans++;
if(temp.left!=null){
queue.offer(temp.left);
}
if(temp.right!=null){
queue.offer(temp.right);
}
}
}
return ans;
}
}
层序遍历 是真的蛮好用的
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int countNodes(TreeNode* root) {
int ans = 0;
if(root == NULL) return ans;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()){
int size = que.size();
for(int i=0;i<size;i++){
TreeNode* node = que.front();
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
ans +=size;
}
return ans;
}
};
解法三:利用满二叉树来求个数
思路:通过递归找到满二叉树,得到满二叉树的个数,再往上求和。
不是很熟练,还得再刷
**/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int countNodes(TreeNode root) {
// 使用完全二叉树的特性
// 判断满二叉树:最左侧的节点的深度==最右侧节点的深度
if(root == null){
return 0;
}
TreeNode left = root.left;
TreeNode right = root.right;
int left_ = 0;
int right_ = 0;
while(left!=null){ // 一致迭代到最后一个左节点,得到其深度
left = left.left;
left_++;
}
while(right!=null){ // 得到最右节点深度。
right = right.right;
right_++;
}
if(left_ == right_){ // 如果相等就是满二叉树
return (2<<left_) - 1; //求满二叉的节点个数,返回到上一级
}
return countNodes(root.left) + countNodes(root.right) + 1;
// 最后依次将左侧的数量和右侧的数量加起来。
}
}**