package question;import java.util.Stack;/*@author: Roderland@create: 2020年08月14日---21:26*/public class Question114 {public void flatten(TreeNode root) {if (root==null) return;Stack<TreeNode> stack = new Stack<>();stack.push(root);TreeNode cur = root;while (!stack.isEmpty()) {TreeNode node = stack.pop();if (node.right!=null) {stack.push(node.right);//node.right=null;}if (node.left!=null) {stack.push(node.left);node.left=null;}if (node!=root) {cur.right = node;cur = node;}}cur.left=null;cur.right=null;}//solution2public void flatten2(TreeNode root) {TreeNode cur = root;while (cur!=null) {if (cur.left!=null) {TreeNode next = cur.left;TreeNode pre = next;while (pre.right!=null) {pre = pre.right;}pre.right = cur.right;cur.right = cur.left;cur.left = null;}cur = cur.right;}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。