Generation

generate functionTue, 07 Feb 2023

写一个二叉树

class BinaryTreeNode{ public $value; public $left; public $right; public function __construct($value = null){ $this->value = $value; $this->left = null; $this->right = null; } public function setLeft($node){ if(!$node instanceof BinaryTreeNode){ throw new Exception('left must be instance of BinaryTreeNode'); } $this->left = $node; } public function setRight($node){ if(!$node instanceof BinaryTreeNode){ throw new Exception('right must be instance of BinaryTreeNode'); } $this->right = $node; } } $tree = new BinaryTreeNode(1); $tree->setLeft(new BinaryTreeNode(2)); $tree->setRight(new BinaryTreeNode(3)); $tree->left->setLeft(new BinaryTreeNode(4)); $tree->left->setRight(new BinaryTreeNode(5)); $tree

Questions about programming?Chat with your personal AI assistant