661
来源:互联网 发布:剑灵天女短发捏脸数据 编辑:程序博客网 时间:2024/06/10 15:12
5.9
很巧妙地做法,采用右根左的中序遍历。
需要注意的是,采用非递归的方法时,sum应该设置为全局变量,刚开始忽略了这一点。
还有就是二叉树的各种遍历的方法,好长时间没有写了,都有点儿生疏了。
/** * 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 the root of binary tree * @return the new root */ public TreeNode convertBST(TreeNode root) { // Write your code here if(root == null){ return root; } inOrder1(root); return root; } // 采用 右根左的遍历顺序 - 非递归的方式 public void inOrder(TreeNode root){ TreeNode bt = root; int sum = 0; LinkedList<TreeNode> list = new LinkedList<TreeNode>(); while(bt != null || !list.isEmpty()){ while(bt != null){ list.push(bt); bt = bt.right; } if(!list.isEmpty()){ bt = list.pop(); bt.val = bt.val + sum; sum = bt.val; bt = bt.left; } } } // 采用 右根左的遍历顺序 - 递归的方式 private int sum = 0; public void inOrder1(TreeNode root){ if(root == null){ return; } inOrder1(root.right); root.val = root.val + sum; sum = root.val; inOrder1(root.left); }}
0 0
- 661
- Leetcode 661
- 661 - Blowing Fuses
- NYOJ 661 亲亲串
- SRM 661 #DIV2
- UVa 661 - Blowing Fuses
- Leetcode 661 Image Smoother
- leetcode 661 Image Smoother
- 661 Image Smoother
- nefu 661 Clockwise 水DP+略几何
- DP SRM 661 Div2 Hard: ColorfulLineGraphsDiv2
- 最小公倍数 SRM 661 Div1 250: MissingLCM
- Topcoder SRM 661 Div1 Easy: MissingLCM
- lintcode(661)Convert BST to Greater Tree
- LintCode 661 Convert BST to Greater Tree
- LeetCode 661 : Image Smoother(python)
- Scade Suite 开发Arinc 661(1)ARINC661
- LeetCode-661:Image Smoother (灰度图像平滑)
- Spring AOP 之 实现APO的多种方式
- dynamic_cast运算符
- eclipse快捷键 包括查找类、方法、变量
- EasyHook远程代码注入
- 维护篇 07. 升级系统固件至 5.6 ❀ 飞塔 (Fortinet) 防火墙
- 661
- Qt:简易的TCP服务器和客户端
- Progress(项目中比较实用进度条)
- VirtualBox的CentOS虚拟机安装增强包
- TensorFlow学习笔记6----tf.contrib.learn Quickstart
- idea报duplicate context path '/'
- win7系统下安装centos7
- 树的存储结构-双亲表示法-代码
- maven实现打包带源代码的jar包