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)的更多相关文章

  1. LeetCode & tree & binary tree

    LeetCode & tree & binary tree 树 & 二叉树 refs https://leetcode.com/problemset/all/?topicSlu ...

  2. leetcode tree相关题目总结

    leetcode tree相关题目小结 所使用的方法不外乎递归,DFS,BFS. 1. 题100 Same Tree Given two binary trees, write a function ...

  3. 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树

    平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...

  4. B-Tree、B+Tree和B*Tree

    B-Tree(这儿可不是减号,就是常规意义的BTree) 是一种多路搜索树: 1.定义任意非叶子结点最多只有M个儿子:且M>2: 2.根结点的儿子数为[2, M]: 3.除根结点以外的非叶子结点 ...

  5. 【Luogu1501】Tree(Link-Cut Tree)

    [Luogu1501]Tree(Link-Cut Tree) 题面 洛谷 题解 \(LCT\)版子题 看到了顺手敲一下而已 注意一下,别乘爆了 #include<iostream> #in ...

  6. 【BZOJ3282】Tree (Link-Cut Tree)

    [BZOJ3282]Tree (Link-Cut Tree) 题面 BZOJ权限题呀,良心luogu上有 题解 Link-Cut Tree班子提 最近因为NOIP考炸了 学科也炸了 时间显然没有 以后 ...

  7. WPF中的Visual Tree和Logical Tree与路由事件

    1.Visual Tree和Logical TreeLogical Tree:逻辑树,WPF中用户界面有一个对象树构建而成,这棵树叫做逻辑树,元素的声明分层结构形成了所谓的逻辑树!!Visual Tr ...

  8. 笔试算法题(39):Trie树(Trie Tree or Prefix Tree)

    议题:TRIE树 (Trie Tree or Prefix Tree): 分析: 又称字典树或者前缀树,一种用于快速检索的多叉树结构:英文字母的Trie树为26叉树,数字的Trie树为10叉树:All ...

  9. WPF中Logical Tree和Visual Tree的区别

    The Logical TreeThe logical tree describes the relations between elements of the user interface. The ...

随机推荐

  1. 江西财经大学第一届程序设计竞赛 I

    链接:https://www.nowcoder.com/acm/contest/115/I来源:牛客网 题目描述 小P和小Q是好朋友,今天他们一起玩一个有趣的游戏. 他们的初始积分都为1,赢的人可以将 ...

  2. day32 线程

    1.     线程是什么,有了进程为什么还要线程 进程有很多优点,它提供了多道编程,让我们感觉我们每个人都拥有自己的CPU和其他资源,可以提高计算机的利用率.很多人就不理解了,既然进程这么优秀,为什么 ...

  3. CSS文件的三种引入方式

    CSS的引入方式共有三种:行内样式.内部样式表.外部样式表. 一.行内样式 使用style属性引入CSS样式. 示例:<h1 style="color:red;">st ...

  4. IKVM:java代码c#调用

    在工作中遇到对接java接口,涉及到java加密或签名问题,.net无法实.就将java代码编辑为dll给.net调用 注:这里只做简单java代码处理,不涉及到复杂的java包 java文件处理: ...

  5. 信息领域热词分析系统--python过滤

    利用python过滤去没用的词语,过滤的词语存储在停用文件中. #创建停用词表 def stopwordlist(): stopwords=[line.strip() for line in open ...

  6. 关于box-shadow、border-radius不兼容ie8的解决办法

    本来从css3兼容ie9+挺好的,可是总有一些共识要求ie8+,于是就有了我们的苦逼的找解决办法.之前在网上查到一些说用 PIE.htc. But 我就是按照他说的写的没有管用.请教了一下别人才会写了 ...

  7. spring配置文件中util:properties和context:property-placeholder

    util:properties和context:property-placeholder标签都可以用来获取外部配置文件中的内容 1.util:properties 它是以声明bean方式来使用,创建了 ...

  8. eclipse中使用git下载项目

    准备工作: 目的:从远程仓库github上down所需的项目 eclipse使用git插件下载github上项目 eclipse版本:eclipse4.5  64位 jdk版本:jdk-1.7 64位 ...

  9. hduoj 2546饭卡

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  10. 自定义Qt组件-通讯模块(P1)

    通讯模块Communicator 通讯模块是整个项目设计中位于最底层的模块,用于处理与串口或网络等设备的通讯,所有设备的通讯通过CommManager类完成,上层软件设计时需要根据comm模块(主要是 ...