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 ...
随机推荐
- 使用YARN修改中国镜像
官网地址 下载安装 https://yarnpkg.com/zh-Hans/ 查看仓库使用地址 yarn config set registry 设置为淘宝镜像 yarn config set r ...
- 工具类_SharedPreferencesUtils
import android.app.Application;import android.content.Context;import android.content.SharedPreferenc ...
- mysql 中decimal中去掉后面多余的0
#去除Decimal后面多余的0 #处理前SELECT '0.12000','1.203010','-0.20' #处理后SELECT 0+CAST('0.12000' AS CHAR),0+CAST ...
- 14-----BBS论坛
BBS论坛(十四) 14.1注册完成跳到上一个页面 (1)front/form.py # front/forms.py __author__ = 'derek' from ..forms import ...
- 转 使用SwingBench 对Oracle RAC DB性能 压力测试
###########说明1: 1 Swingbench 简述 1.1 概述 这是Oracle UK的一个员工在一个被抛弃的项目的基础上开发的.目前稳定版本2.2,最新版本2.3,基于JDK1.5.该 ...
- pyplot
错误: 执行 import matplotlib.pyplot 报错 ImportError: No module named _tkinter, please install the python- ...
- object的equals方法与“==”的使用
官方文档是这么说的:
- DEDE模板中如何运行php脚本和php变量的使用
在使用dede模板的时候,经常会需要直接对dede数据库的底层字段进行处理,如果dede中没有相应的函数的时候,往往就需要我们想办法来处理了. 举例:我想取出数据表addonimages中的某一条记录 ...
- Python 科学工具笔记
Python 科学工具笔记 numpy a = numpy.array([1,2,3,4]);// 创建一个numpy的数组对象 此时a.shape显示的值为(4,); 由此得出结论在一维的数组中, ...
- HDU 4334——Trouble——————【贪心&水题】
Trouble Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...