1. 程式人生 > >lintcode-二叉樹中查詢區間

lintcode-二叉樹中查詢區間

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */


public class Solution {
    /*
     * @param root: param root: The root of the binary search tree
     * @param k1: An integer
     * @param k2: An integer
     * @return: return: Return all keys that k1<=key<=k2 in ascending order
     */
ArrayList<Integer> result = new ArrayList<>(); public List<Integer> searchRange(TreeNode root, int k1, int k2) { // write your code here inOrder(root, k1, k2); return result; } private void inOrder(TreeNode root, int k1, int k2) { if
(root == null)return; inOrder(root.left, k1, k2); if(root.val <= k2 && root.val >= k1) { result.add(root.val); } inOrder(root.right, k1, k2); } }

相關推薦

lintcode-查詢區間

/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNo

線索查詢前驅和後繼的問題

線索二叉樹 結點結構定義如下: 若結點有左子樹,則LChild域仍指向其左孩子; 否則,LChild域指向其某種遍歷序列中的直接前驅結點。 若結點有右子樹,則RChild域仍指向其右孩子; 否則,RChild域指向其某種遍歷序列中的直接後繼結點。 L

求一個的映象/在查詢某個元素X

1.二叉樹映象 void _MirrorTree(Node* root) { //1.空樹,直接返回 if (root==NULL)

LintCode-的最大路徑和

給出一棵二叉樹,尋找一條路徑使其路徑和最大,路徑可以在任一節點中開始和結束(路徑和為兩個節點之間所在路徑上的節點權值之和) 樣例 給出一棵二叉樹: 1 / \ 2 3 返回 6 分析:每條最長路徑都肯定會以某個頂點為跟,然後兩邊是以那

LintCode(632)查詢值最大的節點

問題 Find the maximum node in a binary tree, return the node. Example Given a binary tree: 1 / \ -5 2 / \ / \ 0 3 -4 -5

查找 k1 到 k2區間的節點

roo return push tor style val 二叉樹 traverse nbsp 1 vector<int> res; 2 int key1, key2; 3 4 void traverse(TreeNode

【多次過】Lintcode 94. 的最大路徑和

給出一棵二叉樹,尋找一條路徑使其路徑和最大,路徑可以在任一節點中開始和結束(路徑和為兩個節點之間所在路徑上的節點權值之和) 樣例 給出一棵二叉樹: 1 / \ 2 3 返回 6 解題思路: 利用分治法 解決問題 需要

資料結構 筆記:的結點查詢操作

查詢的方式 -基於資料元素值的查詢 ·BTreeNode<T>* find(const T& value) const -基於結點的查詢 ·BTreeNode<T>* find(TreeNode<T>* node) const 基於資料

資料結構開發(23):結點的查詢、插入、刪除與清除操作

0.目錄 1.二叉樹中結點的查詢操作 2.二叉樹中結點的插入操作 3.二叉樹中結點的刪除操作 4.二叉樹中結點的清除操作 5.小結 1.二叉樹中結點的查詢操作 查詢的方式: 基於資料元素值的查詢 BTreeNode<T>* find(const T&

C++ | 查詢節點(序搜尋)_5

查詢節點(中序搜尋)  /* C++ program to find Inorder successor in a BST */ #include<iostream> using namespace std; struct Node { int data; struct

的最大路徑和-LintCode

描述: 給出一棵二叉樹,尋找一條路徑使其路徑和最大,路徑可以在任一節點中開始和結束(路徑和為兩個節點之間所在路徑上的節點權值之和) 樣例: 給出一棵二叉樹: 1 / \

查詢符合特定條件的值

總結了一下基本用到的公式。 下面用陣列存了下,迴圈求解了 /* 滿二叉樹:一棵深度為h,且有2的(h)次方-1個節點的二叉樹 特點:每一層上的結點數都是最大結點數 它的葉子數是: 2^(h-1) 第k層的結點數是: 2^(h-1) 總結點數是: 2^h-1 (2的k次方減

lintcode——序遍歷

/**  * Definition of TreeNode:  * class TreeNode {  * public:  *     int val;  *     TreeNode *left, *right;  *     TreeNode(int val) {  *         this->

LintCode-分治-的最大路徑和

解題思路:該題目是求二叉樹的最大路徑和,即二叉樹中連結在同一條路徑上的節點值相加的最大值。那麼這個路徑有可能只在左子樹中,有可能從左子樹經過節點到右子樹,也有可能在右子樹中。所以需要判斷。 解題過程:首先新建一個函式,求除節點外,左右子樹節點那條路徑和最大(該路徑是從節點

尋找的最低公共祖先結點----LCA(Lowest Common Ancestor )問題(遞歸)

求解 mon etl 轉換成 right push_back 問題 off == 轉自 劍指Offer之 - 樹中兩個結點的最低公共祖先 題目: 求樹中兩個節點的最低公共祖先。 思路一: ——如果是二叉樹,而且是二叉搜索樹,那麽是可以找到公共節點的。 二叉搜索樹都是排序

binary-tree-inorder-traversal——序遍歷

str () init inorder code while urn value public Given a binary tree, return the inordertraversal of its nodes‘ values. For example:Given

任意兩個結點的距離

case itl wid get ren return roo [] fall 求二叉樹中任意兩個結點的距離實現步驟:計算跟到第一個結點的距離;計算跟到第二個結點的距離;計算lca;計算跟到lca結點的距離;結果為(1) + (2) - 2 * (4),因為重復計算了兩次的

671. Second Minimum Node In a Binary Tree 第二小節點

ati auto www. this 最小 flow app sel special Given a non-empty special binary tree consisting of nodes with the non-negative value, where e

任意兩個節點的最近公共祖先

stc node comm cnblogs blog style == spa 發現 public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p,

的最大路徑和 · Binary Tree Maximum Path Sum

一句話 bsp roo binary pac tree 路徑 num val [抄題]: [思維問題]: [一句話思路]: 用兩次分治。 [輸入量]:空: 正常情況:特大:特小:程序裏處理到的特殊情況:異常情況(不合法不合理的輸入): [畫圖]: 先root-any左右各一