zoukankan      html  css  js  c++  java
  • 剑指offer-面试题8-二叉树的下一个节点-二叉树

    /*
    题目:
    	给定一棵二叉树和其中一个节点,找出中序遍历的下一个节点。
    */
    /*
    思路:
    	两种情况:
    	节点存在右子树:节点右子树的最左节点;
    	节点不存在右子树,节点向上一直找父节点或祖父节点,直到其父节点或祖父节点为其双亲节点的左子树,则next节点为父节点或祖父节点,若没有找到,则无next节点。
    */
    struct TreeNode{
    	int value;
    	TreeNode* left;
    	TreeNode* right;
    	TreeNode* parent;
    }
    
    BinaryTreeNode* GetNext(BinaryTreeNode* pNode){
    	if(pNode == null) return null;
    	
    	BinaryTreeNode* targetNode = pNode;
    	if(targetNode->right != null){
    		targetNode = targetNode->right;
    		while(targetNode->left != null){
    			targetNode = targetNode->left;
    		}	
    	}else if(targetNode->parent != null){
    		targetNode = targetNode->parent;
    		while(targetNode != null && targetNode->parent->right == target){
    			targetNode = targetNode->parent;
    		}
    		if(targetNode != null){
    			targetNode = targetNode->parent;
    		}
    	
    	return targetNode;
    }
    		
    

       

  • 相关阅读:
    2019年春季学期第三周作业
    2019春第二周作业+一些的挑战作业
    查找整数
    寒假作业3编辑总结
    寒假作业2编辑总结
    对自己影响最大的老师
    2019春第九周作业
    2019春第八周作业
    2019春第七周作业
    2019春第六周作业
  • 原文地址:https://www.cnblogs.com/buaaZhhx/p/11822749.html
Copyright © 2011-2022 走看看