Leetcode 623. Add One Row to Tree
来源:互联网 发布:php删除首页标签 编辑:程序博客网 时间:2024/06/09 02:12
题目链接:leetcode 623
Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value v at the given depth d. The root node is at depth 1.
题目很简单,在树的第d层加一层,值为v。递归增加一层就好了。代码如下
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode addOneRow(TreeNode root, int v, int d) { if (null == root) return null; if (1 == d) { TreeNode left = new TreeNode(v); left.left = root; root = left; } else if (2 == d) { TreeNode left = new TreeNode(v); TreeNode right = new TreeNode(v); left.left = root.left; right.right = root.right; root.left = left; root.right = right; } else { addOneRow(root.left, v, d-1); addOneRow(root.right, v, d-1); } return root; }}
阅读全文
1 0
- leetcode-623. Add One Row to Tree
- leetcode 623. Add One Row to Tree
- Leetcode 623. Add One Row to Tree
- [LeetCode] 623. Add One Row to Tree
- leetcode 623. Add One Row to Tree
- leetcode 623 Add One Row to Tree
- [LeetCode] Add One Row to Tree
- LeetCode Add One Row to Tree
- 【LeetCode】623.Add One Row to Tree(Medium)解题报告
- 623. Add One Row to Tree
- 623. Add One Row to Tree
- 623. Add One Row to Tree
- 623. Add One Row to Tree
- 623. Add One Row to Tree
- Add One Row to Tree
- leetcode 623. Add One Row to Tree 深度优先遍历DFS + 很棒的递归
- 623. Add One Row to Tree(Difficulty: Medium)
- Add One Row to Tree问题及解法
- JZOJ 3766【BJOI2014】大融合(lct维护子树大小)
- [ZJOI2006]BZOJ 1003 物流运输
- FFMPEG系列之五:H264视频流直播
- iOS view圆角化的四种方法
- 服务端是如何主动推送信息到客户端的?
- Leetcode 623. Add One Row to Tree
- JDBC连接MYSQL,JDBC增删改查 经典 范例
- jquery对动态生成节点的事件绑定
- lintcode/leetcode由易至难第18题:Find Bottom Left Tree Value
- 使用response对象下载文件
- c++11之initializer_list
- NestedScrollview 嵌套 RecyclerView 滑动卡顿,ScrollView 嵌套 RecyclerView 冲突 解决方案
- 软工文档
- AsyncTask 源码分析