(Easy) Search in Binary Tree - LeetCode

Description:

Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node‘s value equals the given value. Return the subtree rooted with that node. If such node doesn‘t exist, you should return NULL.

For example,

Given the tree:
        4
       /       2   7
     /     1   3

And the value to search: 2

You should return this subtree:

      2
     / \
    1   3

In the example above, if we want to search the value 5, since there is no node with value 5, we should return NULL.

Note that an empty tree is represented by NULL, therefore you would see the expected output (serialized tree format) as [], not null.

Accepted

75,536

Submissions

109,282

Solution:

using Reccrusion

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
       if(root == null || val == root.val)
            return root;
        else
            return val < root.val ? searchBST(root.left, val) : searchBST(root.right, val);

    }
}

原文地址:https://www.cnblogs.com/codingyangmao/p/11429672.html

时间: 2024-11-08 17:46:14

(Easy) Search in Binary Tree - LeetCode的相关文章

Minimum Depth of Binary Tree leetcode java

题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题解: 递归解法急速判断左右两边子树哪个depth最小,要注意如果有个节点只有一边孩子时,不能返回0,要返回另外一半边的depth. 递归解法: 1     public 

Maximum Depth of Binary Tree leetcode java

题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解: 递归解法: 1     public int maxDepth(TreeNode root) {2         if(root==null)3         

Balanced Binary Tree leetcode java

题目: 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. 题解: 采用递归的方法,要记录depth用来比较. 代码如下:

【Leetcode】【Easy】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. 错误的思路(o(N2)时间复杂度): 写一个函数a,用递归遍历的方法,

Maximum Depth of Binary Tree -- leetcode

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 基本思路: 深度优先遍历. 在leetcode上实行执行时间为18ms. /** * Definition for a binary tree node. * struct Tre

[Lintcode]93. Balanced Binary Tree/[Leetcode]

93. Balanced Binary Tree/ 本题难度: Easy Topic: Binary Tree Description 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 n

[Lintcode]97. Maximum Depth of Binary Tree/[Leetcode]104. Maximum Depth of Binary Tree

97. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree 本题难度: Easy Topic: Binary Tree Description Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the f

Balanced Binary Tree Leetcode

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. 这道题一开始的做法比较简单粗暴. public class Solut

110.Balanced Binary Tree Leetcode解题笔记

110.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. 很早以前做的了  准