• 2 Sum in Binary Search Tree


    Given a BST, find 2 nodes in it which sum to a given target

    hint: 

    Inorder traversal + BST + 2 SUM

     * Time : O(N)

     * Space: O(lgN)

      1 class TreeNode0 {
      2     int val;
      3     TreeNode0 left, right;
      4     public TreeNode0(int val) {
      5         this.val = val;
      6     }
      7 }
      8 public class BST2sum {
      9     public void bst2sum(TreeNode0 root, int target) {
     10         TreeNode0 left = root, right = root;
     11         TreeNode0 curl = root, curr = root;
     12         Stack<TreeNode0> stack0 = new Stack<TreeNode0>();
     13         Stack<TreeNode0> stack1 = new Stack<TreeNode0>();
     14         /*
     15          * Init, find the most-left and most-right node
     16          */
     17         while (curl != null || !stack0.isEmpty()) {
     18             if (curl != null) {
     19                 stack0.push(curl);
     20                 curl = curl.left;
     21             } else {
     22                 curl = stack0.pop();
     23                 left = curl;
     24                 curl = curl.right;
     25                 break;
     26             }
     27         }
     28         while (curr != null || !stack1.isEmpty()) {
     29             if (curr != null) {
     30                 stack1.push(curr);
     31                 curr = curr.right;
     32             } else {
     33                 curr = stack1.pop();
     34                 right = curr;
     35                 curr = curr.left;
     36                 break;
     37             }
     38         }
     39         while (right.val >= left.val) {
     40             System.out.println("left: "+left.val + ", right: "+right.val);
     41             if (left.val + right.val == target) {
     42                 System.out.println(left.val + ", " + right.val);
     43                 return;
     44             } else if (left.val + right.val < target) { // move left
     45                 while (curl != null || !stack0.isEmpty()) {
     46                     if (curl != null) {
     47                         stack0.push(curl);
     48                         curl = curl.left;
     49                     } else {
     50                         curl = stack0.pop();
     51                         left = curl;
     52                         curl = curl.right;
     53                         break;
     54                     }
     55                 }
     56             } else if (left.val + right.val > target) { // move right
     57                 while (curr != null || !stack1.isEmpty()) {
     58                     if (curr != null) {
     59                         stack1.push(curr);
     60                         curr = curr.right;
     61                     } else {
     62                         curr = stack1.pop();
     63                         right = curr;
     64                         curr = curr.left;
     65                         break;
     66                     }
     67                 }
     68             }
     69         }
     70         System.out.println("the target has not been found");
     71     }
     72     public static void main(String[] args) {
     73         TreeNode0 node0 = new TreeNode0(0);
     74         TreeNode0 node1 = new TreeNode0(3);
     75         TreeNode0 node2 = new TreeNode0(5);
     76         TreeNode0 node3 = new TreeNode0(6);
     77         TreeNode0 node4 = new TreeNode0(7);
     78         TreeNode0 node5 = new TreeNode0(9);
     79         TreeNode0 node6 = new TreeNode0(13);
     80         TreeNode0 node7 = new TreeNode0(17);
     81         TreeNode0 node8 = new TreeNode0(22);
     82         TreeNode0 node9 = new TreeNode0(23);
     83         node4.left = node2;
     84         node4.right = node6;
     85         node6.left = node5;
     86         node6.right = node8;
     87         node8.left = node7;
     88         node8.right = node9;
     89         node2.left = node0;
     90         node2.right = node3;
     91         node0.right = node1;
     92         BST2sum tree = new BST2sum();
     93         tree.bst2sum(node4, 11);
     94         tree.bst2sum(node4, 15);
     95         tree.bst2sum(node4, 18);
     96         tree.bst2sum(node4, 30);
     97     }
     98 }
     99 /*
    100             7
    101         /       
    102        5         13
    103      /         /   
    104     0     6    9    22
    105                   /  
    106        3           17  23
    107 */
  • 相关阅读:
    Springmvc全局异常处理
    SpringMVC异常处理一
    [GDB7] gdb 的学习
    《Python 第七章》更加抽象
    python问题:IndentationError:expected an indented block错误解决
    [C/C++] C++ 类的学习
    [GCC6] gcc 的学习
    [Python] 列表 list
    [python] 循环与轻量级 pass, del, eval
    《Python 第八章》异常
  • 原文地址:https://www.cnblogs.com/joycelee/p/4516824.html
Copyright © 2020-2023  润新知