首页 > 其他 > 详细

inorder of two BST

时间:2020-02-07 10:13:24      阅读:54      评论:0      收藏:0      [点我收藏+]

给两个bst,把它们的值按照从小到大打印。

 1 public static void print2BSTInorder(TreeNode n1, TreeNode n2, List<Integer> result) {
 2         Stack<TreeNode> stack1 = new Stack<>();
 3         Stack<TreeNode> stack2 = new Stack<>();
 4         
 5         do {
 6             while (n1 != null) {
 7                 stack1.push(n1);
 8                 n1 = n1.left;
 9             } 
10             while (n2 != null) {
11                 stack2.push(n2);
12                 n2 = n2.left;
13             }
14             if (stack2.isEmpty() || stack1.peek().value < stack2.peek().value) {
15                 n1 = stack1.pop();
16                 result.add(n1.value);
17                 n1 = n1.right; 
18             } else {
19                 n2 = stack2.pop();
20                 result.add(n2.value);
21                 n2 = n2.right;
22             }
23         } while (!stack1.isEmpty() || n1 != null || n2 != null || !stack2.isEmpty());
24     }

 

inorder of two BST

原文:https://www.cnblogs.com/beiyeqingteng/p/12271852.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!