extjs,tree,选中leaf,怎么获得它的父节点id,祖父节点id,直到根节点id

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了extjs,tree,选中leaf,怎么获得它的父节点id,祖父节点id,直到根节点id相关的知识,希望对你有一定的参考价值。

如题

click:function(node)
alert(node.id);
var parentNode=node.parentNode;
while(parentNode!=null)
alert(parentNode.id);

parentNode=parentNode.parentNode;

参考技术A click:function(node)
alert(node.id);
var parentNode=node.parentNode;
while(parentNode!=null)
alert(parentNode.id);

parentNode=parentNode.parentNode;

参考技术B  编程语言

 hahaha

参考技术C [网络]叶片; 叶子; 绿叶; 参考技术D xfcxccvcxvcx cxv xcvcxvxcx 第5个回答  2016-05-09 不会,。。。

872. Leaf-Similar Trees

https://leetcode.com/problems/leaf-similar-trees/discuss/152358/Simple-6-lines-Java-StringBuilder-+-traverse-with-explanation



Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form a leaf value sequence.
?
For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).
Two binary trees are considered leaf-similar if their leaf value sequence is the same.
Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.
 



Sol 1 


class Solution {
    // preorder , root, left, right 
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        traverse(root1, sb1);
        traverse(root2, sb2);
        return sb1.toString().equals(sb2.toString()); 
    }
    private void traverse(TreeNode node, StringBuilder sb){
        // base case
        if(node == null) return;
        if(node.left == null && node.right == null) sb.append(node.val + ",");
        traverse(node.left, sb);
        traverse(node.right, sb);
    }
}




class Solution {
    // inorder , left,root, right 
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        traverse(root1, sb1);
        traverse(root2, sb2);
        return sb1.toString().equals(sb2.toString()); 
    }
    private void traverse(TreeNode node, StringBuilder sb){
        // base case
        if(node == null) return;
        traverse(node.left, sb);
        if(node.left == null && node.right == null) sb.append(node.val + ",");
        
        traverse(node.right, sb);
    }
}




Sol 2 
O(logN) Space

https://leetcode.com/problems/leaf-similar-trees/discuss/152329/C++JavaPython-O(logN)-Space


    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        Stack<TreeNode> s1 = new Stack<>(), s2 = new Stack<>();
        s1.push(root1); s2.push(root2);
        while (!s1.empty() && !s2.empty())
            if (dfs(s1) != dfs(s2)) return false;
        return s1.empty() && s2.empty();
    }

    public int dfs(Stack<TreeNode> s) {
        while (true) {
            TreeNode node = s.pop();
            if (node.right != null) s.push(node.right);
            if (node.left != null) s.push(node.left);
            if (node.left == null && node.right == null) return node.val;
        }
    }

 

以上是关于extjs,tree,选中leaf,怎么获得它的父节点id,祖父节点id,直到根节点id的主要内容,如果未能解决你的问题,请参考以下文章

easyui tree获取父节点的父节点

extjs tree 添加复选框

选中树形图treeview的一个节点,怎么得到它的父节点

vue_elementUI_ tree树形控件 获取选中的父节点ID

Extjs Treepanel - 如何删除树的根

EXTJS 4.1 如何在树面板中选择下一个叶节点