《剑指offer》55--二叉树的深度 | 平衡二叉树[C++]

1. 二叉树的深度

二叉树的深度_牛客题霸_牛客网【牛客题霸】收集各企业高频校招笔面试题目,配有官方题解,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力https://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b?tpId=13&tqId=11191&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

题目描述

输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

解题思路

【C++解法】

struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

1、DFS递归大法

class Solution {
public:
    int TreeDepth(TreeNode* pRoot) {
        return pRoot ? 1+max(getDepth(pRoot->left),getDepth(pRoot->right)) : 0;
    }
};

 单独分离DFS:

class Solution {
public:
    int TreeDepth(TreeNode* pRoot) {
        int maxDepth = 0;
        dfs(pRoot, 0, maxDepth);
        return maxDepth;
    }

    void dfs(TreeNode* pRoot, int depth, int& maxDepth) {
        if(!pRoot ) return;
        depth += 1;
        if(depth > maxDepth) maxDepth = depth;
        dfs(pRoot->left, depth, maxDepth);
        dfs(pRoot->right, depth, maxDepth);
    }
};

 2、BFS循环大法

class Solution {
public:
    int TreeDepth(TreeNode* pRoot) {
        if(!pRoot) return 0;
        queue<TreeNode*> q;
        q.push(pRoot);
        int level=0;
        while(!q.empty()){
            int len = q.size();
            level++;
            while(len--){
                TreeNode* tem = q.front(); q.pop();
                if(tem->left) q.push(tem->left);
                if(tem->right) q.push(tem->right);
            }
        }
        return level;
    }
};

【Java解法】

/**
 * 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;
 *     }
 * }
 */

1、DFS递归

class Solution {
    public int TreeDepth(TreeNode root) {
        return root != null
            ? Math.max(maxDepth(root.left), maxDepth(root.right)) + 1 : 0;
    }
}

单独分离DFS:

class Solution {
    public int TreeDepth(TreeNode root) {
        return dfs(root);
    }
    
    private int dfs(TreeNode root) {
        if (root == null) {return 0;}
        return Math.max(dfs(root.left), dfs(root.right)) + 1;
    }
}

2、BFS循环

import java.util.Queue;
class Solution {
    public int TreeDepth(TreeNode root) {
        if (root == null) {return 0;}
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int level = 0;
        while (!q.isEmpty()) {
            int len = q.size();
            level++;
            while (len-- > 0) {
                TreeNode p = q.poll();
                if (p.left != null) {q.offer(p.left);}
                if (p.right != null) {q.offer(p.right);}
            }
        }
        return level;
    }
}

同款LeetCode题目:

LeetCode-104. Maximum Depth of Binary Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree, returnits maximum depth.https://blog.csdn.net/qq_15711195/article/details/122390238

2. 平衡二叉树

平衡二叉树(Balanced Binary Tree),具有以下性质:

它是一棵空树;

或者它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。

《剑指offer》79--判断是不是平衡二叉树[C++][Java]_贫道绝缘子的博客-CSDN博客输入一棵节点数为 n 二叉树,判断该二叉树是否是平衡二叉树。https://blog.csdn.net/qq_15711195/article/details/122388271同款LeetCode题目:

LeetCode-110. Balanced Binary Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given a binary tree, determine if it is height-balanced.https://blog.csdn.net/qq_15711195/article/details/122288238

3. 树中两结点间的最长路径

LeetCode-543. Diameter of Binary Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree, returnthe length of thediameterof the tree. Thediameterof a binary tree is thelengthof the longest path between any two nodes in a tree. This path may or may not pass through theroot.https://blog.csdn.net/qq_15711195/article/details/122392821

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值