C++: Find height of a Binary Tree

给定一棵二叉树, 如何确定这棵二叉树的高度(即树的最大的深度), 是一个很常见的问题。

给下图回顾一下:

关于高度和深度的概念, 参见上图。

NOTE: 高度: 参考节点是距离节点最远的叶子

深度: 参考节点是根节点

寻找二叉树的高度也可以通过一个递归函数(a recursive function)实现, 这依然源于树是一个递归的数据结构。

例如, 对于下图, 我们可以求出根节点的做子树和右子树的高度, 让后将得到的值分别+1 ,然后选择最大数值作为树的高度。 递归的, 要想找到左子树的高度, 首先确定以左子树的节点为根节点, 现在的这个根节点的左子树和右子树的高度, 对于右子树, 同理。 一直递归下去, 直至到达base case, 返回, 最终问题得到求解。

伪代码为:

上述算法的时间复杂度为O(n)(n是binary tree 的节点的数目)

下面给出求解二叉树的高度的函数C++ 代码:

max 函数为:

int max(int a, int b) {
   return (a > b ? a:b);
}

求解高度函数为:

int FindHeight(Node* root) {
   if(root == NULL)
      return -1;
   return max(FindHeight(root -> left), FindHeight(root -> right)) + 1;
}

C++: Find height of a Binary Tree

时间: 2024-10-08 10:19:33

C++: Find height of a Binary Tree的相关文章

LeetCode OJ:Lowest Common Ancestor of a Binary Tree(最近公共祖先)

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w

LeetCode OJ:Flatten Binary Tree to Linked List(捋平二叉树)

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 也就是说用先序遍历的方式将所有的节点都放到右侧来,我这里的方法的主要思想就是每次左侧存在节点的时候就将其原封不动的搬移到右侧来,代码如下: 1 /** 2 * Definition for a binary tr

LeetCode之“树”: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. 这道题难度还好.具体程序(16ms)如下: 1

LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level orde

LeetCode之“树”:Binary Tree Preorder Traversal && Binary Tree Inorder Traversal && Binary Tree Postorder Traversal

Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 递归解

LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)

Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively?中序遍历二叉树,递归遍历当然很容易,题目还要求不用递归,下面给出两种方法: 递归: 1 /**

LeetCode OJ:Binary Tree Zigzag Level Order Traversal(折叠二叉树遍历)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zigz

LeetCode OJ:Binary Tree Right Side View(右侧视角下的二叉树)

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3, 4].

LeetCode OJ:Binary Tree Paths(二叉树路径)

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 简单的遍历查找路径问题,代码如下: 1 /** 2 * Definition for a binary tree node. 3 * str