LeetCode: Convert BST to Greater Tree
生活随笔
收集整理的這篇文章主要介紹了
LeetCode: Convert BST to Greater Tree
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
利用好BST的特性,解起來并不難
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 private int sum = 0; 12 public TreeNode convertBST(TreeNode root) { 13 if (root == null) return null; 14 convertBST(root.right); 15 root.val += sum; 16 sum = root.val; 17 convertBST(root.left); 18 return root; 19 } 20 }?
轉載于:https://www.cnblogs.com/yingzhongwen/p/6844549.html
總結
以上是生活随笔為你收集整理的LeetCode: Convert BST to Greater Tree的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: **设计模式中的常用原则
- 下一篇: 【Gradle】配置详解(持续更正补充)