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;
    }
    		
    

       

  • 相关阅读:
    elementUI form select验证问题
    Echart--基本属性
    Echart--饼状图
    Echart--折线图
    Echart--多个柱形图
    Echart--单个柱形图
    Echart--圆型图
    构析函数和构造函数
    心情
    图片
  • 原文地址:https://www.cnblogs.com/buaaZhhx/p/11822749.html
Copyright © 2011-2022 走看看