zoukankan      html  css  js  c++  java
  • 剑指offer38:输入一棵二叉树,求该树的深度

    1 题目描述

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

    2 思路和方法

      深度优先搜索,每次得到左右子树当前最大路径,选择其中较大者并回溯。int len = left>right?left+1:right+1;    // 当前最大路径

    3 C++ 核心代码

     1 /*
     2 struct TreeNode {
     3     int val;
     4     struct TreeNode *left;
     5     struct TreeNode *right;
     6     TreeNode(int x) :
     7             val(x), left(NULL), right(NULL) {
     8     }
     9 };*/
    10 class Solution {
    11 public:
    12     int TreeDepth(TreeNode* pRoot){
    13         if (pRoot == nullptr)
    14             return 0;
    15         int left = TreeDepth(pRoot->left);
    16         int right = TreeDepth(pRoot->right);
    17         int maxLen = left>right?left+1:right+1;    // 当前最大路径
    18 
    19         return maxLen;
    20     }
    21 };
    View Code

    4 C++完整代码

     1 /*
     2 
     3 
     4 输入:
     5 第一行输入有n,n表示结点数,结点号从1到n。根结点为1。 n <= 10。
     6 
     7 接下来有n行,每行有两个个整型a和b,表示第i个节点的左右孩子孩子。a为左孩子,b为右孩子。当a为-1时,没有左孩子。当b为-1时,没有右孩子。
     8 
     9 输出:
    10 输出一个整型,表示树的深度。
    11 
    12 样例输入:
    13 3
    14 2 3
    15 -1 -1
    16 -1 -1
    17 样例输出:
    18 2
    19 */
    20 /*
    21 
    22 思路: 用递归,根节点的深度等于max(左孩子的深度,右孩子的深度)。
    23 */
    24 
    25 //此题用数组存储树节点
    26 
    27 #include<stdio.h>
    28 #include<stdlib.h>
    29 struct TreeNode{
    30     int pLeftChild;
    31     int pRightChild;
    32 };
    33 //递归实现
    34 int depthInBTree(TreeNode* pRoot, int index){
    35     if (pRoot == NULL || index == -1){//直到左右孩子时,返回0
    36         return 0;
    37     }
    38     int leftDepth = depthInBTree(pRoot, pRoot[index].pLeftChild);
    39     int rightDepth = depthInBTree(pRoot, pRoot[index].pRightChild);
    40     return (leftDepth>rightDepth ? leftDepth : rightDepth) + 1;
    41 }
    42 int main(void){
    43     int n;
    44     while (scanf("%d", &n) != EOF&&n>0 && n <= 10){
    45         TreeNode *pRoot = (TreeNode *)malloc(n*sizeof(TreeNode));
    46         if (pRoot == NULL){
    47             exit(EXIT_FAILURE);
    48 
    49         }
    50         for (int i = 0; i<n; i++){
    51             int leftIndex;
    52             int rightIndex;
    53             scanf("%d%d", &leftIndex, &rightIndex);
    54             if (leftIndex != -1){
    55                 pRoot[i].pLeftChild = leftIndex - 1;
    56             }
    57             else{
    58                 pRoot[i].pLeftChild = -1;
    59             }
    60             if (rightIndex != -1){
    61                 pRoot[i].pRightChild = rightIndex - 1;
    62             }
    63             else{
    64                 pRoot[i].pRightChild = -1;
    65             }
    66         }
    67 
    68         int depth = depthInBTree(pRoot, 0);
    69         printf("%d
    ", depth);
    70 
    71     }
    72 
    73     system("pause");
    74     return 0;
    75 }
    View Code

    参考资料

    https://blog.csdn.net/libin1105/article/details/48395021

  • 相关阅读:
    为什么会需要消息队列(MQ)?
    RBAC用户角色权限设计方案
    转:jquery 父、子页面之间页面元素的获取,方法的调用
    LeetCode Wiggle Subsequence
    LeetCode Longest Arithmetic Sequence
    LeetCode Continuous Subarray Sum
    LeetCode Maximum Length of Repeated Subarray
    LeetCode Is Subsequence
    LeetCode Integer Break
    LeetCode Largest Sum of Averages
  • 原文地址:https://www.cnblogs.com/wxwhnu/p/11421376.html
Copyright © 2011-2022 走看看