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 ...
随机推荐
- Oracle 树形SQL语句,SYS_CONNECT_BY_PATH 函数
转一个SYS_CONNECT_BY_PATH 函数的例子.推断原表应该是这样: Child Parent ------------------------ ...
- 杭电ACM hdu 2079 选课时间 (模板)
Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合.你来帮帮他吧.(xhd认为一样学分的课没区别) Input输入 ...
- my15_ mysql binlog格式从mixed修改为row格式
由于主库繁忙,就在从库上修改binlog格式 1. 从库切日志mysql> flush logs;Query OK, 0 rows affected (0.00 sec) mysql> f ...
- Java——socket
Server: import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public cla ...
- JavaSE---多线程---线程的生命周期
1.线程的生命周期:新建.就绪.运行.阻塞.死亡 2.运行状态线程进入阻塞: 1.1 调用sleep方法主动放弃: 1.2 调用线程的suspend方法将线程挂起,不推荐使用: 1.3 线程调用一个阻 ...
- Python collections
#count对象 Only 2.7 from collections import Counter #统计字母出现的次数 Counter('hello world') Counter(['red', ...
- LCS与打印路径
/* LCS */ #include<bits/stdc++.h> using namespace std; const int maxn = 1000; int dp[maxn][max ...
- HDU 4334——Trouble——————【贪心&水题】
Trouble Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- Golang笔记(二)面向对象的设计
Golang笔记(二)面向对象的设计 Golang本质还是面向过程的语言,但它实现了一些OOP的特性,包括抽象.封装.继承和多态. 抽象和封装 Golang和C语言一样以struct为数据结构核心,不 ...
- [转]Asp.net Core中使用Session
本文转自:http://www.cnblogs.com/sword-successful/p/6243841.html 前言 2017年就这么悄无声息的开始了,2017年对我来说又是特别重要的一年. ...