814. Binary Tree Pruning(leetcode) (tree traverse)
https://leetcode.com/contest/weekly-contest-79/problems/binary-tree-pruning/ -- 814 from leetcode
tree traverse (post traverse) left right and root
the model
void traverse(node){
if(node.left!=null) traverse(node.left);
if(node.right!=null) traverse(node.right);
//do something, current node is the deep leaf(left or right)
}
another model
int traverse(node){
if(node.left!=null) l = traverse(node.left); // value is its left child
if(node.right!=null) r = traverse(node.right); // value of its right child
//do something, current node is the deep leaf(left or right)
return node.val;
}
This problem we use second model and check if the children are null(tag ==2 : two children are nulls)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode pruneTree(TreeNode root) {
int val = traverse(root);
if(val == 0 && root.left==null && root.right==null) root=null;
return root;
}
int traverse(TreeNode node){//when for
//lright root
int tag = 0;
if(node.left!=null){
int l = traverse(node.left);//retur the vale of next layer(left child)
if(l==0) {
node.left = null;
tag ++;
} }else tag++;
if(node.right!= null){
int r = traverse(node.right);
if(r== 0) {
node.right = null;
tag ++;
}
}else tag ++;
//root
if(node.val==0 && tag==2)//remove this node
return 0;
else return 1;
}
}
ziji youdian cai(newbie) I only figured out one problem from the contest.
814. Binary Tree Pruning(leetcode) (tree traverse)的更多相关文章
- LeetCode & tree & binary tree
LeetCode & tree & binary tree 树 & 二叉树 refs https://leetcode.com/problemset/all/?topicSlu ...
- leetcode tree相关题目总结
leetcode tree相关题目小结 所使用的方法不外乎递归,DFS,BFS. 1. 题100 Same Tree Given two binary trees, write a function ...
- 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树
平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...
- B-Tree、B+Tree和B*Tree
B-Tree(这儿可不是减号,就是常规意义的BTree) 是一种多路搜索树: 1.定义任意非叶子结点最多只有M个儿子:且M>2: 2.根结点的儿子数为[2, M]: 3.除根结点以外的非叶子结点 ...
- 【Luogu1501】Tree(Link-Cut Tree)
[Luogu1501]Tree(Link-Cut Tree) 题面 洛谷 题解 \(LCT\)版子题 看到了顺手敲一下而已 注意一下,别乘爆了 #include<iostream> #in ...
- 【BZOJ3282】Tree (Link-Cut Tree)
[BZOJ3282]Tree (Link-Cut Tree) 题面 BZOJ权限题呀,良心luogu上有 题解 Link-Cut Tree班子提 最近因为NOIP考炸了 学科也炸了 时间显然没有 以后 ...
- WPF中的Visual Tree和Logical Tree与路由事件
1.Visual Tree和Logical TreeLogical Tree:逻辑树,WPF中用户界面有一个对象树构建而成,这棵树叫做逻辑树,元素的声明分层结构形成了所谓的逻辑树!!Visual Tr ...
- 笔试算法题(39):Trie树(Trie Tree or Prefix Tree)
议题:TRIE树 (Trie Tree or Prefix Tree): 分析: 又称字典树或者前缀树,一种用于快速检索的多叉树结构:英文字母的Trie树为26叉树,数字的Trie树为10叉树:All ...
- WPF中Logical Tree和Visual Tree的区别
The Logical TreeThe logical tree describes the relations between elements of the user interface. The ...
随机推荐
- 停止mysql服务
停止mysql服务windowsnet stop mysql (service mysqld stop win10不能用) linux /etc/init.d/mysqld stop
- js随机生成[n,m)的数字(不包括m)
Math.random();//随机生成0到1的数字 Math.floor();//取小整 Math.floor(Math.random()*(最大值 - 最小值) + 最小值) 生成2到8的数:Ma ...
- Xshell上Linux上传下载文件
Xshell上的Linux想要进行文件的上传和下载可以使用以下命令: #rz //将本地的文件上传到Linux服务器,执行后会弹出选择文件的框 #sz filename //将 filename 这个 ...
- Cookie和Session入门(一)
目录一)背景介绍二)Cookie机制三)Session机制四)两者比较五)参考资料链接一)背景介绍Cookie与Session是常用的会话跟踪技术.1.Cookie通过在客户端记录信息确定用户身份,S ...
- python 读取文件使用chunksize后逐块迭代操作
chunkers=pd.read_csv('dd.csv',chunksize=10000) tot=pd.Series([]) for piece in chunkers: tot=tot.add( ...
- tcp的半连接与完全连接队列(二)
队列及参数 server端的半连接队列(syn队列) 在三次握手协议中,服务器维护一个半连接队列,该队列为每个客户端的SYN包开设一个条目(服务端在接收到SYN包的时候,就已经创建了request_s ...
- 查看服务器配置信息prtdiag与systeminfo实用命令
UNIX(SUN服务器)bash-2.05# prtdiag -v系统配置: Sun Microsystems sun4u Sun Fire V890系统时钟频率:150 MHz内存大小:3276 ...
- 转载【Ubuntu】Ubuntu14.04虚拟机调整窗口大小自适应VMware14窗口
Ubuntu屏幕居中一小块,很不好看 查看-–> 自动调整大小—>自动适应客户机/自动适应窗口 切一下就可以把Ubuntu图的界面大小调的和VMware窗口自适应了 效果: 转载 自h ...
- SpringBoot如何集成Jedis
添加jedis依赖 在项目pom.xml文件中添加依赖 <!-- 添加jedis依赖 --> <dependency> <groupId>redis.clients ...
- ASP.NET前端调用后台方法
<script> function MyConfirm() { if (confirm('存在重复记录,覆盖点继续,不覆盖追加保存点取消')) { ...