Get Level of a node in a Binary Tree

Given a Binary Tree and a key, write a function that returns level of the key.

For example, consider the following tree. If the input key is 3, then your function should return 1. If the input key is 4, then your function should return 3. And for key which is not present in key, then your function should return 0.

思路:DFS,递归来做。 或者BFS都行吧。来个递归先

 1 public class DepthOfRandomNode {
 2     public int getDepth(TreeNode root, TreeNode target) {
 3         return helper(root, target, 0);
 4     }
 5     private static int helper(TreeNode root, TreeNode target, int level) {
 6         if (root == null) {
 7             return 0;
 8         }
 9         if (root.val == target.val) {
10             return level + 1;
11         }
12         int left = helper(root.left, target, level + 1);
13         int right = helper(root.right, target, level + 1);
14         if (left != 0) {
15             return left;
16         }else if(right != 0) {
17             return right;
18         }else{
19             return 0;
20         }
21     }
22     public static void main(String[] args) {
23         TreeNode root = new TreeNode(1);
24         TreeNode left1 = new TreeNode(2);
25         TreeNode right1 = new TreeNode(3);
26         TreeNode left21 = new TreeNode(4);
27         TreeNode left22 = new TreeNode(5);
28         TreeNode right21 = new TreeNode(6);
29         TreeNode right22 = new TreeNode(7);
30         root.left = left1;
31         root.right = right1;
32         left1.left = left21;
33         left1.right = right21;
34         right1.left = left22;
35         right1.right = right22;
36         DepthOfRandomNode test = new DepthOfRandomNode();
37         System.out.println(test.getDepth(root,root));
38     }
39 }
时间: 2024-11-09 01:49:47

Get Level of a node in a Binary Tree的相关文章

LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9

671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有节点中的第二小的值.如果第二小的值不存在的话,输出 -1. 每日一算法 2019/5/12Day 9 LeetCode671. Second Minimum Node In a B

[leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

[leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree 链接 leetcode 描述 ??Given two binary trees original and cloned and given a reference to a node target in the original tree. ??The cloned tree is a copy of the original tr

[LeetCode] Second Minimum Node In a Binary Tree

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.

671. Second Minimum Node In a Binary Tree 二叉树中第二小节点

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly twoor zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. G

[leetcode-671-Second Minimum Node In a Binary Tree]

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.

【easy】671. Second Minimum Node In a Binary Tree

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.

671. Second Minimum Node In a Binary Tree

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.

1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

Given two binary trees original and cloned and given a reference to a node target in the original tree. The cloned tree is a copy of the original tree. Return a reference to the same node in the cloned tree. Note that you are not allowed to change an

[LeetCode]Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 re