zoukankan      html  css  js  c++  java
  • 剑指OFFER 二叉树的下一个结点

    剑指OFFER 二叉树的下一个结点

    /*
    struct TreeLinkNode {
        int val;
        struct TreeLinkNode *left;
        struct TreeLinkNode *right;
        struct TreeLinkNode *next;
        TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {
            
        }
    };
    */
    class Solution {
    public:
        TreeLinkNode* target_node = NULL;
        TreeLinkNode* res_node = NULL;
        bool ok = false;
        
        void recur(TreeLinkNode* node)
        {
            if(node == NULL){
                return ;
            }
            recur(node->left);
            if(ok == true){
                res_node = node;
                ok = false;//avoid next entering
            }
            if(node == target_node){
                ok = true;
            }
            recur(node->right);
        }
        
        TreeLinkNode* GetNext(TreeLinkNode* pNode)
        {
            //handle special case
            if(pNode == NULL)return NULL;
            
            //initialize 
            target_node = pNode;
            
            //get root node
            TreeLinkNode* root = pNode;
            while(root->next != NULL){
                root = root->next;
            }
            
            //recurly traverse to find the next node
            recur(root);
            return res_node;
        }
    };
    
  • 相关阅读:
    React 使用链表遍历组件树
    React diff 算法
    JavaScript 对象操作
    前端路由hash
    动画运动曲线
    ajax跨域问题
    js版本状态模式
    装饰者模式AOP
    swipe源码循环索引
    组合模式--超级宏命令
  • 原文地址:https://www.cnblogs.com/virgildevil/p/12261667.html
Copyright © 2011-2022 走看看