package question;import java.util.ArrayList;import java.util.List;import java.util.Stack;/*@author: Roderland@create: 2020年08月14日---16:48*/public class Question897 {public static void main(String[] args) {TreeNode t1 = new TreeNode(1);TreeNode t2 = new TreeNode(2);TreeNode t3 = new TreeNode(3);t1.left=t2;t1.right=t3;System.out.println(new Question897().increasingBST(t1));}List<TreeNode> list = new ArrayList<>();public TreeNode increasingBST(TreeNode root) {//bst1(root);bst2(root);list.get(0).left=null;list.get(list.size()-1).right=null;for (int i = 1; i < list.size(); i++) {list.get(i).left=null;list.get(i-1).right=list.get(i);}return list.get(0);}//递归中序public void bst1(TreeNode root) {if (root==null) return;bst1(root.left);list.add(root);bst1(root.right);}//迭代中序public void bst2(TreeNode root) {if (root==null) return;Stack<TreeNode> stack = new Stack<>();stack.push(root);while (!stack.isEmpty()) {TreeNode left = stack.peek().left;if (left!=null) {stack.peek().left=null;stack.push(left);continue;}TreeNode node = stack.pop();list.add(node);if (node.right!=null) {stack.push(node.right);node.right=null;}}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。